﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplace.CApReplaceWhatValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.TcCommands.TcReplace;

internal class CApReplaceWhatValidator : ITxValidator
{
  private ITxTypeFilter m_TxFilter;
  protected CUiReplaceDialog _replaceDialog;

  internal CApReplaceWhatValidator(CUiReplaceDialog replaceDialog)
  {
    this._replaceDialog = replaceDialog;
  }

  public void SetTypeFilter(ITxTypeFilter filter) => this.m_TxFilter = filter;

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      foreach (ITxObject itxObject2 in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject2, out string _))
        {
          itxObject1 = itxObject2;
          break;
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj) => obj.Name;

  public static bool IsResource(ITxObject obj)
  {
    return TxTcUtilities.IsTcAnyPlatform && TxEngineeringDataInternal.ObjectIsResourceInstance(obj);
  }

  public static bool IsCompoundEquipment(ITxObject selectedObject)
  {
    return selectedObject is ITxComponent && (selectedObject as ITxComponent).IsEquipment && selectedObject is ITxObjectCollection;
  }

  protected bool IsReplacementAllowed(TxObjectList what, ITxObject with)
  {
    bool flag = true;
    HashSet<ITxObjectCollection> objectCollectionSet = new HashSet<ITxObjectCollection>();
    foreach (ITxObject itxObject in (Collection<ITxObject>) what)
    {
      foreach (ITxPlanningInstance instance in (Collection<ITxPlanningInstance>) (TxTcAuthoring.GetPlanningObject(itxObject) as ITxPlanningInstance).Prototype.Instances)
        objectCollectionSet.Add(((ITxPlanningObject) instance).EngineeringRepresentation.Collection);
    }
    foreach (ITxObjectCollection objectCollection in objectCollectionSet)
      flag = TxTcAuthoring.CanBeChildOf(with, (ITxObject) objectCollection) & flag;
    HashSet<ITxOperation> itxOperationSet = new HashSet<ITxOperation>();
    foreach (ITxObject itxObject in (Collection<ITxObject>) what)
    {
      TxPlanningResource planningObject = TxTcAuthoring.GetPlanningObject(itxObject) as TxPlanningResource;
      itxOperationSet.UnionWith(((IEnumerable) planningObject.OperationsToWhichAssigned).Cast<ITxOperation>());
    }
    foreach (ITxOperation itxOperation in itxOperationSet)
      flag = TxOperationExtensionMethods.CanBeAssigned(itxOperation, with) & flag;
    return flag;
  }

  public virtual bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    bool flag = false;
    errorMessage = (string) null;
    using (new TxCallMessages.Collector((object) "CApReplaceWhatValidator.IsValidObject"))
    {
      if (obj == null)
        errorMessage = StringTable.REPLACE_PROTOTYPE_INVALID_OBJECT_ERROR;
      else if (obj is ITxComponent)
      {
        flag = CApReplaceWhatValidator.IsResource(obj) || CApReplaceWhatValidator.IsCompoundEquipment(obj);
        if (flag)
        {
          flag = obj.CanBeModified;
          if (!flag)
          {
            errorMessage = string.Join("\n", TxCallMessages.GetMessages());
            if (string.IsNullOrEmpty(errorMessage))
              errorMessage = StringTable.REPLACE_PROTOTYPE_INVALID_OBJECT_ERROR;
          }
          else if (((Collection<ITxObject>) this._replaceDialog.grInstResources.Objects).Count > 0)
          {
            if (this._replaceDialog.pbReplaceBy.Object != null)
            {
              flag = this.IsReplacementAllowed(this._replaceDialog.grInstResources.Objects, this._replaceDialog.pbReplaceBy.Object);
              if (!flag)
              {
                errorMessage = string.Join("\n", TxCallMessages.GetMessages());
                if (string.IsNullOrEmpty(errorMessage))
                  errorMessage = StringTable.REPLACE_PROTOTYPE_INVALID_OBJECT_ERROR;
              }
            }
          }
        }
        else
          errorMessage = StringTable.REPLACE_PROTOTYPE_NOT_A_RESOURCE_ERROR;
      }
      else
        errorMessage = StringTable.REPLACE_PROTOTYPE_NOT_A_COMPONENT_ERROR;
    }
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    bool flag;
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      flag = false;
      foreach (ITxObject itxObject in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject, out errorMessage))
        {
          flag = true;
          break;
        }
      }
    }
    else
    {
      errorMessage = "Invalid Object";
      if (text == "")
        errorMessage = "Entity name is empty";
      flag = false;
    }
    return flag;
  }
}
